using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;

public static class TransformExtensions
{
    public static void AddChild(this Transform transform, Transform child)
    {
        child.SetParent(transform);
    }

    public static Transform CreateChild(this Transform transform, string name)
    {
        var go = new GameObject(name);
        var child = go.transform;
        transform.AddChild(child);
        return child;
    }

    public static void RemoveAllChilds(this Transform transform)
    {
        var childCount = transform.childCount;
        for(int i=0; i<childCount; i++)
        {
            GameObject.Destroy(transform.GetChild(i).gameObject);
        }
    }

    public static void SetPos(this Transform transform, Vector3 wPos)
    {
        transform.position = wPos;
    }

    public static void SetPos(this Transform transform, float x, float y, float z)
    {
        transform.position = new Vector3(x, y, z);
    }

    public static void GetPos(this Transform transform, out float x, out float y, out float z)
    {
        var wPos = transform.position;
        x = wPos.x; y = wPos.y; z = wPos.z;
    }

    public static void SetSizeDelta(this Transform transform, float width, float height)
    {
        var rt = transform as RectTransform;
        rt.sizeDelta = new Vector2(width, height);
    }

    public static void SetSizeWidth(this Transform transform, float width)
    {
        var rt = transform as RectTransform;
        rt.sizeDelta = new Vector2(width, rt.sizeDelta.y);
    }

    public static void SetSizeHeight(this Transform transform, float height)
    {
        var rt = transform as RectTransform;
        rt.sizeDelta = new Vector2(rt.sizeDelta.x, height);
    }

    public static void SetAnchoredMin(this Transform transform, float x, float y)
    {
        var rt = transform as RectTransform;
        rt.anchorMin = new Vector2(x, y);
    }

    public static void SetAnchoredMax(this Transform transform, float x, float y)
    {
        var rt = transform as RectTransform;
        rt.anchorMax = new Vector2(x, y);
    }

    public static void SetOffsetMin(this Transform transform, float x, float y)
    {
        var rt = transform as RectTransform;
        rt.offsetMin = new Vector2(x, y);
    }

    public static void SetOffsetMax(this Transform transform, float x, float y)
    {
        var rt = transform as RectTransform;
        rt.offsetMax = new Vector2(x, y);
    }

    public static void ResetRTS(this Transform transform)
    {
        transform.localScale = Vector3.one;
        transform.localRotation = Quaternion.Euler(Vector3.zero);
        transform.localPosition = Vector3.zero;
    }

    public static void SetLayer(this Transform transform, int layer, bool recursive = false)
    {
        transform.gameObject.SetLayer(layer, recursive);
    }

    public static void SetLayer(this Transform transform, string name, bool recursive = false)
    {
        transform.gameObject.SetLayer(name, recursive);
    }

    public static void SetScale(this Transform transform, float x, float y, float z)
    {
        transform.localScale = new Vector3(x, y, z);
    }

    public static void SetScale(this Transform transform, float scale)
    {
        transform.localScale = new Vector3(scale, scale, scale);
    }

    public static void SetScaleX(this Transform transform, float x)
    {
        var scale = transform.localScale;
        transform.localScale = new Vector3(x, scale.y, scale.z);
    }

    public static void SetScaleY(this Transform transform, float y)
    {
        var scale = transform.localScale;
        transform.localScale = new Vector3(scale.x, y, scale.z);
    }

    public static void SetScaleZ(this Transform transform, float z)
    {
        var scale = transform.localScale;
        transform.localScale = new Vector3(scale.x, scale.y, z);
    }

    public static void GetScale(this Transform transform, out float x, out float y, out float z)
    {
        var scale = transform.localScale;
        x = scale.x; y = scale.y; z = scale.z;
    }

    public static void GetScaleX(this Transform transform, out float x)
    {
        var scale = transform.localScale;
        x = scale.x;
    }

    public static void GetScaleY(this Transform transform, out float y)
    {
        var scale = transform.localScale;
        y = scale.y;
    }

    public static void GetScaleZ(this Transform transform, out float z)
    {
        var scale = transform.localScale;
        z = scale.z;
    }

    public static void SetFlipX(this Transform transform, bool flip)
    {
        var scale = transform.localScale;
        scale.x = Mathf.Abs(scale.x) * (flip ? -1 : 1);
        transform.localScale = scale;
    }

    public static void SetFlipY(this Transform transform, bool flip)
    {
        var scale = transform.localScale;
        scale.y = Mathf.Abs(scale.y) * (flip ? -1 : 1);
        transform.localScale = scale;
    }

    public static void SetFlipZ(this Transform transform, bool flip)
    {
        var scale = transform.localScale;
        scale.z = Mathf.Abs(scale.z) * (flip ? -1 : 1);
        transform.localScale = scale;
    }

    public static string GetHierarchyPath(this Transform transform)
    {
        var arr = new List<string>();
        
        var node = transform;
        while (node != null)
        {
            arr.Add(node.name);
            node = node.parent;
        }
        arr.Reverse();

        return string.Join("/", arr);
    }
}
